package me.leiusual.sharing.ui.home.adapter

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.view_holder_home_search_store_parent.*
import me.leiusual.sharing.api.model.storeproductquery.Product
import me.leiusual.sharing.api.model.storesearch.StoreContent
import me.leiusual.sharing.util.extension.setImageUrl

/**
 * author: leiusual
 * create on: 2019-07-30
 * description:
 */
class HomeSearchStoreParentViewHolder(
    override val containerView: View,
    private val clickListener: (StoreContent) -> Unit,
    clickChildListener: (Product) -> Unit
) : RecyclerView.ViewHolder(containerView), LayoutContainer, View.OnClickListener {

  private lateinit var storeContent: StoreContent

  init {
    containerView.setOnClickListener(this)

    recycler_view.adapter = HomeSearchStoreChildAdapter(clickChildListener)
  }

  fun bind(storeContent: StoreContent) {
    this.storeContent = storeContent

    image_view.setImageUrl(storeContent.logoUrl)

    text_view_name.text = storeContent.name

    val stringBuilder = StringBuilder()
    stringBuilder.append("等级：")
    stringBuilder.append(storeContent.levelText)
    stringBuilder.append("\n地址：")
    stringBuilder.append(storeContent.address)
    stringBuilder.append("\n简述：")
    stringBuilder.append(storeContent.introduction)
    text_view_detail.text = stringBuilder.toString()

    text_view_total.text = "共${storeContent.productCount}件商品"

    val adapter = recycler_view.adapter as HomeSearchStoreChildAdapter
    adapter.submitList(storeContent.topProducts)
  }

  override fun onClick(v: View?) {
    clickListener(storeContent)
  }
}